package com.wannengbang.storemobile.homepage;


import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ethanhua.skeleton.Skeleton;
import com.ethanhua.skeleton.SkeletonScreen;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.wannengbang.storemobile.R;
import com.wannengbang.storemobile.base.BaseActivity;
import com.wannengbang.storemobile.base.DataCallBack;
import com.wannengbang.storemobile.bean.BaseResponseBean;
import com.wannengbang.storemobile.bean.ProductClassifyListBean;
import com.wannengbang.storemobile.dialog.AddClassifyDialog;
import com.wannengbang.storemobile.homepage.adapter.ProductClassifyListAdapter;
import com.wannengbang.storemobile.homepage.model.HomePageModelImpl;
import com.wannengbang.storemobile.homepage.model.IHomePageModel;
import com.wannengbang.storemobile.utils.FastClickUtils;
import com.wannengbang.storemobile.utils.ToastUtil;
import com.wannengbang.storemobile.widget.AppTitleBar;
import com.wannengbang.storemobile.widget.CommonNoTitleDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 商品分类
 */
public class ProductClassifyActivity extends BaseActivity implements OnRefreshLoadMoreListener {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    private IHomePageModel homePageModel;
    private ProductClassifyListAdapter listAdapter;
    private List<ProductClassifyListBean.DataBean.ItemListBean> beanList;
    private SkeletonScreen skeletonScreen;
    private CommonNoTitleDialog.Builder dialogBuild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_classify);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        refreshLayout.setRefreshHeader(new ClassicsHeader(mActivity));//设置Header
        refreshLayout.setRefreshFooter(new ClassicsFooter(mActivity));//设置Footer
        refreshLayout.setOnRefreshLoadMoreListener(this);
    }

    public void initData() {
        homePageModel = new HomePageModelImpl();

        beanList = new ArrayList<>();
        listAdapter = new ProductClassifyListAdapter(beanList);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);
        listAdapter.setOnCallBackListener((type, position) -> {
            if (FastClickUtils.isFastClick()) {
                return;
            }
            if (type == 1) {
                ProductClassifyListBean.DataBean.ItemListBean itemListBean = beanList.get(position);
                AddClassifyDialog dialog = AddClassifyDialog.newInstance("2", itemListBean.getId() + "", itemListBean.getName(), itemListBean.getSort() + "");
                dialog.show(getSupportFragmentManager(), "dialog");
                dialog.setOnCallBackListener(() -> {
                    requestData();
                });
            } else {
                dialogBuild = new CommonNoTitleDialog.Builder(mActivity);
                dialogBuild.setMessage("是否删除此分类?");
                dialogBuild.setPositiveButton("确定", v -> {
                    dialogBuild.dismiss();
                    requestProductClassifyDelete(beanList.get(position).getId() + "", position);
                });
                dialogBuild.setNegativeButton("取消", v -> dialogBuild.dismiss());
                dialogBuild.setCancelable(false);
                dialogBuild.show();
            }

        });

        skeletonScreen = Skeleton.bind(recyclerView)
                .adapter(listAdapter)
                .color(R.color.shimmer_color_2)
                .shimmer(true)
                .angle(20)
                .frozen(false)
                .duration(1000)
                .count(20)
                .load(R.layout.item_skeleton_income_list)
                .show();

        requestData();
    }

    public void requestData() {
        homePageModel.requestProductClassify(new DataCallBack<ProductClassifyListBean>() {
            @Override
            public void onSuccessful(ProductClassifyListBean bean) {
                beanList.clear();
                if (bean.getData().getItemList() != null && bean.getData().getItemList().size() > 0) {
                    beanList.addAll(bean.getData().getItemList());
                }
                if (beanList.size() > 0) {
                    llNoData.setVisibility(View.GONE);
                } else {
                    llNoData.setVisibility(View.VISIBLE);
                }
                listAdapter.notifyDataSetChanged();
                skeletonScreen.hide();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    private void requestProductClassifyDelete(String id, int position) {
        homePageModel.requestProductClassifyDelete(id, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort("删除成功");
                beanList.remove(position);
                listAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick(R.id.tv_commit)
    public void onViewClicked() {
        if (FastClickUtils.isFastClick()) {
            return;
        }
        AddClassifyDialog dialog = AddClassifyDialog.newInstance("1", "", "", "");
        dialog.show(getSupportFragmentManager(), "dialog");
        dialog.setOnCallBackListener(() -> {
            requestData();
        });
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

    }
}
